package com.spm.librarianmanagersystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.BookMapper;
import com.spm.librarianmanagersystem.mapper.UserManageMapper;
import com.spm.librarianmanagersystem.pojo.Borrow;
import com.spm.librarianmanagersystem.pojo.Borrowing;
import com.spm.librarianmanagersystem.pojo.Member;
import com.spm.librarianmanagersystem.service.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 苏成
 * @Date: 2022/04/07/18:37
 * @Description:
 */

@Service("userManageService")
public class UserManageServiceImpl implements UserManageService {

    @Autowired
    private UserManageMapper userManageMapper;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public JSONArray getUser(int user_id) {
        Member member = userManageMapper.getMember(user_id);
        JSONArray array = new JSONArray();
        array.add( JSON.toJSON(member) );
        return array;
    }

    @Override
    public boolean updateUser(Map map) {
        userManageMapper.updateMember(map);
        return true;
    }

    @Override
    public boolean addUser(Map map) {
        userManageMapper.addMember(map);
        return true;
    }

    @Override
    public boolean deleteUser(List user_ids) {
        // 这个地方含有了外键，我们先删除fine表之中的记录然后
        for(Object user_id : user_ids)
        {
            userManageMapper.deleteFine((Integer) user_id);
            userManageMapper.deleteBorrow( (Integer) user_id );
            userManageMapper.deleteRes((Integer) user_id );
            userManageMapper.deleteMember((Integer) user_id);
        }
        return true;
    }

    @Override
    public boolean addInfBook(Map map) {
        return false;
    }

    @Override
    public JSONArray getAllMember(int page_size, int current_page) {
        List<Member> memberList = userManageMapper.getAll();
        JSONArray array = new JSONArray();
        int all_size = memberList.size();

        // 根据是不是最后一页判断这一页有几本书
        JSONObject object1 = new JSONObject();
        int all = (int) Math.ceil( (double) all_size / page_size);
        int total = current_page < all ? page_size : ( all_size % page_size );
        object1.put("total", total);
        array.add(object1);

        int start = ( current_page - 1 )  * page_size;

        for( int i = 0 ; i < total && start + i < all_size ; i++ ){
            Member m = memberList.get(i+start);
            array.add(JSON.toJSON(m));
        }
        return array;
    }

    @Override
    public JSONArray getAllBorrow() {
        List<Borrow> allborrow = userManageMapper.getAllBorrow();

        Borrow b;
        Borrowing borrowing;
        JSONArray array = new JSONArray();


        for( int i = 0; i < allborrow.size(); ++i ){
            b = allborrow.get(i);
//            Date act_time = b.getActReturnTime();
            Date due_time = b.getDueTime();
            borrowing = new Borrowing();

//            boolean flag;
//            if( act_time.after( due_time ) ){
//                flag = true;
//                int b_id = b.getBorrowingNumber();
//                borrowing.setFine( bookMapper.getfine( b_id ) );
//            }else{
//                flag = false;
//                borrowing.setFine(0);
//            }
            borrowing.setFine(0);
//            borrowing.setOverdue(flag);
            borrowing.setBookId(b.getBookId());
            borrowing.setBorrowingNumber(b.getBorrowingNumber());
            borrowing.setBorrowStartTime(b.getBorrowStartTime());
            borrowing.setDueTime(b.getDueTime());
            borrowing.setRenewed(b.getRenewed());
            borrowing.setUserId(b.getUserId());
            borrowing.setBookName(bookMapper.getABook(b.getISBN()).getBookName());
            array.add( JSON.toJSON(borrowing) );

        }
        return array;
    }
}
